package com.controller.user;

import com.domain.Discuss;
import com.domain.Space;
import com.domain.User;
import com.server.DiscussService;
import com.server.SpaceService;
import com.server.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping(path="/users")
public class personalController {

    @Resource(name = "userService")
    private UserService userService;
    @Resource(name = "spaceService")
    private SpaceService spaceService;
    @Resource(name = "discussService")
    private DiscussService discussService;

    //通用页面跳转控制器
    @RequestMapping(value="/{formName}")
    public String f(@PathVariable String formName){
        return formName;
    }

    @RequestMapping(value="/user-dongtai")
    public String userDongtai(Integer id,Model model,HttpSession session){
        int memberId=id;
        System.out.println(id);
        List<Space> space=spaceService.selectSpaceByUserId(id);
        model.addAttribute("space",space);
        User user1=userService.selectUserById(id);
        model.addAttribute("user1",user1);
        List<Discuss> discuss=discussService.selectDiscussByMemberId(memberId);
        model.addAttribute("discuss",discuss);
        return "/user-dongtai";
    }

    @RequestMapping(value = "/user-personal-center")
    public String userPersonal(Integer id,Model model){
        User user=userService.selectUserById(id);
        model.addAttribute("user",user);
        return "user-personal-center";
    }

    @RequestMapping(value = "/upload-Image",method=RequestMethod.POST)
    public String uploadImage(MultipartFile file,HttpSession session,@RequestParam("id") Integer userId,Model model){
        User user=userService.selectUserById(userId);
        if(file.getName()!=""){
            String userLogo=file.getOriginalFilename();
            userLogo=userLogo.substring(0,userLogo.lastIndexOf("."));
            System.out.println(userLogo);
            user.setUserLogo(userLogo);
            //相对路径的图片文件上传：
            String imagepath=session.getServletContext().getRealPath("/WEB-INF/images");
            System.out.println("头像上传相对路径imagepath===="+imagepath);
            File newImage=new File(imagepath+"\\"+userLogo+".jpg");
            if(!newImage.getParentFile().exists()){
                newImage.getParentFile().mkdirs();
            }
            try {
                file.transferTo(newImage);
                int result=userService.updateUser(user);
                if(result<=0){
                    System.out.println("上传失败2！");
                    return "user-personal-center";
                }
                session.setAttribute("user",user);
                model.addAttribute("user",user);
                return "user-personal-center";
            }  catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        System.out.println("上传失败1！");
        return "user-personal-center";
    }
    @RequestMapping(value = "/update-user")
    public String updateUser(@RequestParam("id") Integer id,User user,Model model,HttpSession session){
        User user1=userService.selectUserById(id);
        user.setUserLogo(user1.getUserLogo());
        userService.updateUser(user);
        session.setAttribute("user",user);
        model.addAttribute("user",user);
        return "user-personal-center";
    }

    @RequestMapping(value = "/update-password")
    public String updatePassword(@RequestParam("id") String id,User user,
                                 String password,String rePassword) throws IOException {
        System.out.println(password+" "+rePassword);
        if(!password.equals(rePassword)){
            //<script>alert('两次密码输入不正确！')</script>
            System.out.println("更改失败1！");
            return "user-update-password";
        }else if(password.length()<6||password.length()>20){
            //<script>alert('密码长度为6-20位')</script>
            System.out.println("更改失败2！");
            return "user-update-password";
        }else{
            user.setPassword(password);
            System.out.println(user.getPassword());
            userService.updateUser(user);
            return "welcome";
        }
    }

    @RequestMapping(value = "/discuss-title")
    public String discussTitle(Integer memberId,String title,Model model){
        Discuss discuss=discussService.selectDiscussByTitleAndMemberId(title,memberId);
        model.addAttribute("discuss",discuss);
        return "space-discuss";
    }

    @RequestMapping(value = "/contribute-title")
    public String contributeTitle(Integer memberId,String title,Model model){
//        contribute contribute=contributeService.selectContributeByTitleAndMemberId(title,memberId);
//        model.addAttribute("contribute",contribute);
        return "space-contribute";
    }

}
